package com.sicau.socialpracticesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sicau.socialpracticesystem.entity.po.Points;
import com.sicau.socialpracticesystem.entity.po.PointsItem;
import com.sicau.socialpracticesystem.entity.po.User;
import com.sicau.socialpracticesystem.mapper.PointsMapper;
import com.sicau.socialpracticesystem.service.PointsItemService;
import com.sicau.socialpracticesystem.service.PointsService;
import com.sicau.socialpracticesystem.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 积分表 服务实现类
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-04-26
 */
@Service
@Slf4j
public class PointsServiceImpl extends ServiceImpl<PointsMapper, Points> implements PointsService {

    @Resource
    PointsMapper pointsMapper;
    @Resource
    PointsItemService pointsItemService;
    @Resource
    UserService userService;

    @Override
    public Points queryByUserId(Integer userId) {
        if (userId == null){
            return null;
        }
        LambdaQueryWrapper<Points> queryWrapper = new LambdaQueryWrapper<Points>()
                .eq(Points::getUserId, userId);
        return pointsMapper.selectOne(queryWrapper);
    }

    @Override
    public void consumePointById(Integer id, Double consumePoints) {
        Points points = pointsMapper.selectById(id);
        if ( (points.getAllPoints() - points.getUsedPoints()) < consumePoints){
            throw new RuntimeException("更新points异常,积分不够,id为"+ id +" 消耗分数为"+ consumePoints);
        }
        LambdaUpdateWrapper<Points> updateWrapper = new LambdaUpdateWrapper<Points>()
                .set(Points::getUsedPoints, points.getUsedPoints() + consumePoints)
                .eq(Points::getId, id);
        pointsMapper.update(null, updateWrapper);
    }

    @Override
    public void addPointsByUserNumber(Map<Integer, Double> map) {
        map.forEach((userNumber, addPoints) -> {
            pointsMapper.addPointsByUserNumber(userNumber, addPoints);
        });
    }

    @Override
    public void initPoint(Integer userNumber) {
        if (userNumber == null){
            return;
        }
        User user = userService.queryUserByNumber(userNumber);
        if (user == null) {
            log.error("学号为 {} 的用户并未创建", userNumber);
            return;
        }
        List<PointsItem> pointsItemList = pointsItemService.queryAllByUserNumber(userNumber);
        Points points = new Points();
        points.setAllPoints(0d);
        if (pointsItemList != null && pointsItemList.size() > 0){
            pointsItemList.forEach(pointsItem -> {
                points.setAllPoints(points.getAllPoints() + pointsItem.getPoints());
            });
        }
        points.setUsedPoints(0d);
        points.setUserNumber(userNumber);
        points.setUserId(user.getId());

        pointsMapper.insert(points);
    }
}
